package com.sduept.nwld.dataserver.rest;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.LoginTime;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/login")
public class LoginTimeService {
	
	@Autowired
	private SuperDAO dao;
	@Autowired
	private PersonnelManager pm;
	
	private List<LoginTime> loginTimelist = new ArrayList<LoginTime>();
	

	@GetMapping("/addLoginTimeByCode/{username}")
	public void addLoginTimeByCode(@PathVariable("username") String username) {
		String code = username.replace("\"", "");
		String esql = "from LoginTime where code = '" + code + "'";
		loginTimelist = dao.findByComplexCondition(esql, LoginTime.class);
		LoginTime bean;
		if(loginTimelist.size() == 0) {
			bean = new LoginTime();
			bean.setCode(code);
			bean.setLoginTime(1);
			PersonnelEntityBean person = pm.findPersonnelByCode(code);
			bean.setName(person.getName());
			dao.createOrUpdate(bean, LoginTime.class);
		} else {
			bean = loginTimelist.get(0);
			bean.setLoginTime(bean.getLoginTime() + 1);
			dao.createOrUpdate(bean, LoginTime.class);
		}
	}
}
